//
//  RXVistorView.swift
//  weibo
//
//  Created by uncle-R on 15/12/15.
//  Copyright © 2015年 uncle-R. All rights reserved.
//

import UIKit
protocol RXVistorViewDelegate :NSObjectProtocol{
    //登录按钮被点击
    func visitorViewLoginBtnClick()
    //注册按钮被点击.
    func visitorViewResgiterBtnClick()

}

class RXVistorView: UIView {
    //声明一个代理.
    weak var delegate: RXVistorViewDelegate?
    
    //代码创建必调用.
    override init(frame: CGRect) {
        super.init(frame: frame)
        //添加子控件.
         backgroundColor = UIColor(white: 237 / 255.0, alpha: 1)
        addUI()
    }
  
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
  
    private func addUI(){
        //添加子控件
        addSubview(iconView)
        addSubview(converView)
        addSubview(homeView)
        addSubview(message)
        addSubview(registerButton)
        addSubview(loginButton)
        //**监听登录 和 注册按钮//
        registerButton.addTarget(self, action: "registerBtnClick", forControlEvents: UIControlEvents.TouchDragInside)
        
        registerButton.addTarget(self, action: "loginBtnClick", forControlEvents: UIControlEvents.TouchDragInside)
        
        
        
        //去除translatesAutoresizingMaskIntoConstraints (会与AutoLayout冲突)
        iconView.translatesAutoresizingMaskIntoConstraints = false
        homeView.translatesAutoresizingMaskIntoConstraints = false
        message.translatesAutoresizingMaskIntoConstraints = false
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        converView.translatesAutoresizingMaskIntoConstraints = false
        
        //设置约束.
        //设置转轮的位置.
        addConstraint( NSLayoutConstraint(item: iconView ,attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView ,attribute: NSLayoutAttribute.CenterY , relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant:-40))
        //
        //设置房子的约束
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        //消息label CenterX和轮转CenterX重合
        addConstraint(NSLayoutConstraint(item: message, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: message, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        //设置消息label的宽度.
        addConstraint(NSLayoutConstraint(item: message, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 240))
        
        //登录按钮 height = 35 Width = 100;
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: message, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: message, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        
        //注册按钮 height = 35 Width = 100;
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: message, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: message, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
       // 设置阴影
        addConstraint(NSLayoutConstraint(item: converView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        
         addConstraint(NSLayoutConstraint(item: converView, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
//        //上
         addConstraint(NSLayoutConstraint(item: converView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
//        //下.
         addConstraint(NSLayoutConstraint(item: converView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 10))
        
    }
    
    //MARK: - 按钮的登录与注册---
    func registerBtnClick(){
        self.delegate?.visitorViewResgiterBtnClick()
      
    }
    
    func loginBtnClick(){
    //通知代理授权登录.
        self.delegate?.visitorViewLoginBtnClick()

    }
    
    //MARK:懒加载
    private lazy var iconView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    private lazy var homeView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //提示文字
    private lazy var message :UILabel = {
        let lable = UILabel()
        lable.text = "关注一些人,看看这里有什么惊喜!"
        lable.textColor = UIColor.darkGrayColor()
        lable.font = UIFont.systemFontOfSize(16)
        lable.numberOfLines = 0
        return lable
    }()
    //注册按钮   common_button_white_disable
    private lazy var registerButton :UIButton = {
        let button = UIButton()
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState:  UIControlState.Normal)
         button.setTitle("注册", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        return button
    }()
    //登录按钮
    private lazy var loginButton: UIButton = {
        let button = UIButton()
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitle("登陆", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        return button
    }()
    //遮罩.
//    private lazy var coverView : UIImageView = {
//        
//        let coverView  = UIImageView()
//        coverView.image = UIImage(named: "visitordiscover_feed_mask_smallicon")
//        return coverView
//    
//    }()
    private lazy var converView  : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    
    //MARK:转轮的核心动画
    //转轮旋转的动画
    func startRotationAnimation(){
        
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        //设置参数.
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 20
        //动画完成时不移除
        anim.removedOnCompletion = false
        //给图层添加核心动画
        iconView.layer.addAnimation(anim, forKey: nil)
    }
    
    /// 暂停旋转
    func pauseAnimation() {
        // 记录暂停时间
        let pauseTime = iconView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil)
        
        // 设置动画速度为0
        iconView.layer.speed = 0
        
        // 设置动画偏移时间
        iconView.layer.timeOffset = pauseTime
    }
    
    /// 恢复动画
    func resumeAnimation() {
        // 获取暂停时间
        let pauseTime = iconView.layer.timeOffset
        
        // 设置动画速度为1
        iconView.layer.speed = 1
        
        // 重置偏移时间
        iconView.layer.timeOffset = 0
        
        // 重置开始时间
        iconView.layer.beginTime = 0
        
        // 计算开始时间
        let timeSincePause = iconView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil) - pauseTime
        
        // 设置开始时间
        iconView.layer.beginTime = timeSincePause
    }
    
    ///提供一个方法设置其他三个页面的访客视图
    func setupVisitorInfo(imageName:String,messageL:String){
        
        //替换转轮的图片
        iconView.image = UIImage(named: imageName)
        //更换文字
        message.text = messageL
        //隐藏房子
        homeView.hidden = true
        //遮罩移到最底部
        sendSubviewToBack(converView)
        
        
    }

    
    
    
    
    
    
}
